import com.dmotiko.general.*
import com.dmotiko.chabeli.*
import com.dmotiko.util.*
import mx.utils.Delegate;

class ChabeliMainPhoto
extends PhotoSlider {
	
	private var initWidth:Number;
	private var firstPhotoMask:MovieClip;
	
	private var fadeOut:Function;
	private var fadeIn:Function;
		
	private function ChabeliMainPhoto() {
		super();
		FRAMES_TIMER = 10;
		aSlides.push("img/home1.jpg");
		aSlides.push("img/home2.jpg");
		aSlides.push("img/home3.jpg");
		aSlides.push("img/home4.jpg");
		
		mcLoader._y = Stage.height / 2;
		mcLoader._xscale = Stage.width * 100 / (mcLoader._width /2);
		startSlider();
		ChabeliSite.getApp().addEventListener( WebSite.SECTION_CHANGED, Delegate.create( this, this.sectionChanged ));
	}
	
	private function sectionChanged(evnt):Void {
		//ChabeliSite.getApp().log("chabeliMainPhoto sectionChanged last= "+evnt.last+" | section= "+evnt.section);
		this.timerController.stopEffect();
		switch(ChabeliSite.getApp().getSection()) {
			
			case ChabeliSite.PRODUCTOS:
			MovieEffects.fadeOut();
			this.fadeOut();
			//bSlideShow = false;
			break;
						
			default:
			MovieEffects.fadeIn();
			this.fadeIn();
			//if (!bSlideShow) nextPhoto();
			//bSlideShow = true;
			break;
		}
	}
	
	private function startSlider():Void {
		if ( ChabeliSite.getApp().getSection() != ChabeliSite.HOME ) return;
		//ChabeliSite.getApp().log("ChabeliMainPhoto | startSlider");
		super.startSlider();
	}
	
	private function nextPhoto(){
		if ( ChabeliSite.getApp().getSection() != ChabeliSite.HOME ) return;
		//ChabeliSite.getApp().log("ChabeliMainPhoto | nextPhoto");
		super.nextPhoto();
	}
	
	public function onLoadStart(target) {
		super.onLoadStart(target);
		//ChabeliSite.getApp().log("PhotoSlider | onLoadStart= " + target);
	}
	
	private function loadComplete(evnt, data){
		//ChabeliSite.getApp().log("PhotoSlider | loadComplete= " + evnt.target);
		MovieEffects.fadeIn();
		MovieEffects.fadeOut();
		MovieEffects.interpolate();
		firstPhotoMask = this.attachMovie("loadBar", "mcMask", this.getNextHighestDepth());
		var clip = firstPhotoMask;
		clip._width = mcSlideShow._width;
		clip._x = mcSlideShow._x;
		clip._y = mcSlideShow._y + (mcSlideShow._height - clip._height) / 2;
		mcSlideShow._alpha = 100;
		mcSlideShow.setMask(clip);
		
		var _ss:MovieClip = mcSlideShow;
				
		clip.interpolator = clip.interpolate( { prop: "_height", init: clip._height, end: mcSlideShow._height, bind: this, listener: firstPhotoShowEnd, ratio: 5 } );
		
		clip.onEnterFrame = function() {
			this._y = _ss._y + (_ss._height - this._height) / 2;
		}
		
	}
	
	private function firstPhotoShowEnd(){
		super.firstPhotoShowEnd();
		mcSlideShow.setMask(null);
		firstPhotoMask.removeMovieClip();
	}
	
	private function photoShowEnd(clip){
		super.photoShowEnd();
		clip.setMask(null);
		firstPhotoMask.removeMovieClip();
	}
	
	private function loadNextPhoto( nextSlide ) {
		if ( Stage.width <= 1280 ) {
			//ChabeliSite.getApp().log("ChabeliMainPhoto | loadMediumPic= " + getMediumPath(aSlides[nCount]));
			mcl.loadClip( getMediumPath(aSlides[nCount]), nextSlide);
		} else {
			mcl.loadClip(aSlides[nCount], nextSlide);	
		}
	}
	
	private function getMediumPath( path:String ):String {
		var sPath:String = path;
		return sPath.substr(0, sPath.length - 4) + "_medium" + sPath.substr(sPath.length - 4, sPath.length-1);
	}
	
	public function onLoadInit(target) {
		//ChabeliSite.getApp().log("PhotoSlider | onLoadInit= " + target);
		if(!bFirst) {
			bFirst = true;
			target._alpha = 0;
			lastSlide = target;
								
		} else {
			lastSlide.fadeOut( { rate: 7 });
			lastSlide = target;
			
			firstPhotoMask = this.attachMovie("loadBar", "mcMask", this.getNextHighestDepth());
			var clip = firstPhotoMask;
			clip._width = target._width;
			clip._x = target._x;
			clip._y = target._y + (target._height - clip._height) / 2;
			target._alpha = 100;
			target.setMask(clip);
			
			var _ss:MovieClip = target;
					
			clip.interpolator = clip.interpolate( { prop: "_height", init: clip._height, end: target._height, bind: this, listener: photoShowEnd, ratio: 10 } );
			
			clip.onEnterFrame = function() {
				this._y = _ss._y + (_ss._height - this._height) / 2;
			}
			
		}		
		
		Stage.addListener(target);
		target.onResize = function(){
			var nWidth:Number = Stage.width;
			if (nWidth <= 800) nWidth = 800;
			
			var initWidth:Number = target._width;
			target._width = nWidth;
			target._height = target._width * target._height / initWidth;
		}
		target.onResize();
				
	}
	
}